{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "359ee489-8a67-4d27-b0dd-7106159915ed",
   "metadata": {},
   "source": [
    "Chapter 06\n",
    "# 身份运算符\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "af6578ae-de7d-4647-8794-75ae61ec5377",
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "a = [1, 2, 3]\n",
    "b = a  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1b92f76c-9fd1-40b6-899d-8a4a6f0e5add",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 视图 b 引用 a 的内存地址\n",
    "c = [1, 2, 3]\n",
    "d = a.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8a6c4d2-4989-4e79-b36a-88ad2137f679",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a is b)        \n",
    "# 输出 True，因为 a 和 b 引用同一个内存地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0d9fa7f7-8745-48cc-9d9a-737fd22c6717",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a is not c)    \n",
    "# 输出 True，因为 a 和 c 引用不同的内存地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6068edfd-e2cc-4a20-bdb2-7cd60589f28a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a == c)       \n",
    "# 输出 True，因为 a 和 c 的值相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2381071f-bdae-41e9-8550-bbbd84c29d67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a is not d)   \n",
    "# 输出 True，因为 a 和 d 引用不同的内存地址"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dec1e286-bb1c-4db9-b6c0-fc253090070d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a == d)      \n",
    "# 输出 True，因为 a 和 d 的值相等"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "474d4925-8179-4198-a043-c249698d70cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, [3, 4]]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_2_layers = [1, 2, [3, 4]]\n",
    "a_2_layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "21eac3e3-0da0-44b6-936f-5ca6ee20ee0c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, [3, 4]]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d_2_layers = a_2_layers.copy() \n",
    "d_2_layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a95747a8-5ffb-4c79-84b5-abd715fc7a27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, [3, 4]]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "e_2_layers = copy.deepcopy(a_2_layers) \n",
    "e_2_layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a58e1a2b-5e36-44e8-a66f-373d4e60fe1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(a_2_layers is d_2_layers)\n",
    "print(a_2_layers[2] is d_2_layers[2]) # 请特别关注!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e84baceb-7b9a-4b83-99db-50b29e4f9133",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(a_2_layers is e_2_layers)\n",
    "print(a_2_layers[2] is e_2_layers[2])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
